/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.jitag.model.config;

import java.util.List;

import com.db4o.ObjectContainer;

import sk.jp.jitag.dao.db4o.BaseEntity;
import sk.jp.jitag.dao.db4o.Column;
import sk.jp.jitag.dao.db4o.CascadeOnDelete;

public class MediaType extends BaseEntity {

    private static final Column _TYPECLASS = new Column("typeClass");

    private MediaTypeClass typeClass;
    @CascadeOnDelete
    private List<FileFormat> fileFormats = createList(0);

    //----------------------------------------------------------------------

    public MediaType(MediaTypeClass typeClass) {
        checkNotNull(_TYPECLASS, typeClass);

        this.typeClass = typeClass;
    }

    public MediaTypeClass getTypeClass() {
        return typeClass;
    }

    public List<FileFormat> getFileFormats() {
        return fileFormats;
    }

    public MediaType addFileFormat(FileFormat fileFormat){
        if(!fileFormats.contains(fileFormat)){
            fileFormats.add(fileFormat);
        }
        return this;
    }

    public MediaType removeFileFormat(FileFormat fileFormat){
        fileFormats.remove(fileFormat);
        return this;
    }

    public MediaType removeFileFormat(String fileFormatLabel){
        FileFormat fileFormatToRemove = null;
        for (FileFormat fileFormat : fileFormats) {
            if(fileFormat.getLabel().equals(fileFormatLabel)){
                fileFormatToRemove = fileFormat;
            }
        }
        if(fileFormatToRemove != null){
            removeFileFormat(fileFormatToRemove);
        }
        return this;
    }

    /**
     * Refresh fields with unique values from database.
     * @param container
     * @return allways true
     */
    public boolean objectCanNew(ObjectContainer container){
        typeClass = getActiveEnumInstance(typeClass, container);
        return true;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("MediaType");
        sb.append("{typeClass=").append(typeClass);
        sb.append(", fileFormats=").append(fileFormats);
        sb.append('}');
        return sb.toString();
    }
}
